package.cpath = "./skynet/luaclib/?.so;".. package.cpath
package.path = "./skynet/lualib/?.lua;./app/common/lib/?.lua;./testclient/?.lua;" .. package.path

if _VERSION ~= "Lua 5.3" then
	error "Use lua 5.3"
end

local socket = require "client.websocket"
local protobuf = require "protobuf"
local parser = require "parser"

local md5 = require "md5"

parser.register("./app/proto/primary.proto")
parser.register("./app/ttznode/ttz.proto")

local platform2login_key = "p2l_salt"

local decode = require "protopack"

--local fd = assert(socket.connect("127.0.0.1", 8191))
local fd = assert(socket.connect("127.0.0.1", 8191))
local is_logined = false

local function send_package(fd, pack)
	-- 使用了websocket ，会自己组织帧格式的
	socket.send(fd, pack)
end

local function recv_package()
	local r , istimeout= socket.recv(fd, 100)
	if not r then
		return nil
	end
	if r == ""  and (not istimeout ) then
		error "Server closed"
	end
	return r
end


local function send_request(msgname, msg)
	if msgname ~= "heart" then
		print(msgname)
	    print(msg)
	end
	
	local str = decode.c2spack(protobuf, msgname, msg)
	send_package(fd, str)
end

local last = ""

local function print_request(name, args)
	print("REQUEST", name)
	if args then
		for k,v in pairs(args) do
			print(k,v)
		end
	end
end

local bet_btn_list = {1101,1102,1103}
local function random_bet()
	local score = 100
	local cnt = math.random(1,6)

	for i = 1,cnt do
		for k,id in pairs(bet_btn_list) do
			local r = math.random(0,1)
			if r == 1 then
				send_request("ttz.doaction",{action_type = 1, bet_item_id = id, bet_score = score})
			end
		end
	end
	
end

local function print_response(msghead, msgbody)
	if msghead.msgname == "heartR" then
		return
	end

	print('------ begin response --------')
	if msghead then
		for k,v in pairs(msghead) do
			print(k,v)
			if v == "authR" and msgbody then
				gate_ip = "127.0.0.1"
				rid = msgbody.rid  
				gateport = msgbody.gateport     
				expiretime = msgbody.expiretime
				gatetoken = msgbody.gatetoken
				username = msgbody.username
				cpass = msgbody.cpass
				fd = assert(socket.connect(gate_ip, gateport))
				if fd then
					send_request("entergame", {rid=rid, uid=uid, gatetoken=gatetoken,expiretime = expiretime,agencyrid = agencyrid, clientver = "0000"})
				end
			elseif v == "ttz.todoN" then
				random_bet()
			end
		end
	end
	if msgbody then
		for k,v in pairs(msgbody) do
			print(k,v)
			if type(v) == "table" then
				print("-- table: ", k )
				for i,j in pairs(v) do
					print("  --", i,j)
					if type(j) == "table" then
						print("------ subtable: ", i)
						for kk,vv in pairs(j) do
							print("     ----",kk,vv)
							if type(vv) == "table" then
								print("----------- subsubtable: ", kk)
								for kkk,vvv in pairs(vv) do
									print("         ------",kkk,vvv)
								end
							end
						end
					end
				end
			end
		end
	end
	print('------ end response --------')
end

local function dispatch_package()
	while true do
		local v
		v = recv_package()
		if not v  or v == "" then
			break
		end
		print_response(decode.s2cunpack(protobuf, v))
	end
end

local function gentoken(username)
	local tokentime = os.time()
	local newtoken = md5.sumhexa(username .. tokentime .. platform2login_key ) .. tokentime
	return newtoken
end

math.randomseed(os.time())
math.random()

local count = 1

while true do
	dispatch_package()
	local cmd = socket.readstdin()
	if cmd then
		if cmd == "awx" then
			local newtoken = gentoken("wxid_1314")
			send_request("authwx", {username = "wxid_1314", refresh_token = "123456", rolename = "abc", logintoken = newtoken})
		elseif cmd == "auo" then
			local newtoken = gentoken("wx_1111")
			send_request("authother", {username = "wx_1111", cpass = "123456", logintoken = newtoken})
		elseif cmd == "aur" then
			local newtoken = gentoken("17122333344")
			send_request("authuser", {username = "17122333344", cpass = "123456", logintoken = newtoken})
		elseif cmd == "agt" then
			local username = "46634e1c0013cdea0172" .. math.random(100000,999999)
			local newtoken = gentoken(username)
			send_request("authguest", {username = username, logintoken = newtoken})
		elseif cmd == "et" then
			send_request("ttz.entertable", {level_type = 1})
		elseif cmd == "st" then
			send_request("ttz.sitdowntable", {})
		elseif cmd == "rd" then
			send_request("ttz.gameready", {})
		elseif cmd == "lv" then
			send_request("ttz.leavetable", {})
		elseif tonumber(cmd) then
			local num = tonumber(cmd)
			local id = math.floor(num / 10000)
			local score = num % 10000
			send_request("ttz.doaction", {action_type = 1, bet_item_id = id, bet_score = score})
		elseif cmd == "cs" then
			send_request("ttz.doaction", {action_type = 2})
		elseif cmd == "rb" then
			send_request("ttz.doaction", {action_type = 3})
		elseif cmd == "sz" then
			send_request("ttz.doaction", {action_type = 4, cur_upbanker_money = 300000})
		elseif cmd == "xz" then
			send_request("ttz.doaction", {action_type = 5})
		elseif cmd == "heart" then
			send_request("ttz.nodeheart", {nodeallname = "ttz"})
		elseif cmd == "gl" then
			send_request("ttz.getlevellist", {nodename = "ttz"})
		elseif cmd == "gpl" then
			for i = 1,100 do
				send_request("ttz.getplayerlist", {player_num = 200})
			end
		elseif cmd == "gbl" then
			send_request("ttz.getupbankerlist", { })
		elseif cmd == "ba1" then
			send_request("bindwithdrawaccount", { account_type = 1, realname = "张三", accountname ="abc"})
		elseif cmd == "ba3" then
			send_request("bindwithdrawaccount", { account_type = 3, realname = "张三", bankname = "建设银行", accountname ="6200000000000000"})
		elseif cmd == "tx1" then
			send_request("withdrawmoney", { account_type = 1, accountname = "abc", value = 10000})
		elseif cmd == "tx3" then
			send_request("withdrawmoney", { account_type = 3, accountname = "6200000000000000", value = 10000})
		elseif cmd == "gtx" then
			send_request("getwithdrawrecord", { })
		elseif cmd == "cpwd" then
			send_request("changephonepassword", { phonenum = "17122333344", verification_code = "642441", cpass = "123456"})
		elseif cmd == "pa" then
			send_request("phoneauth", { phonenum = "17122333344", verification_code = "227147", realname = "李四", cpass = "46634e1c0013cdea0172"})
		elseif cmd == "gvc" then
			send_request("getverificationcode", { phonenum = "17122333344"})
		elseif cmd == "http" then
			send_request("httprequest", { request_url = "http://aaa.com/home/api/index?rid=688995&sign=8728ea486fae7c5fa29951b218ccbb80&strtime=1541820170"})
		elseif cmd == "gm" then
			send_request("givemoney", { target_rid = 650958, value = 100000})
		elseif cmd =="ct" then
			local table_conf = {
				table_type = 4,
				openlottery_mode = 1,
				retain_to_num = 10,
				up_banker_money = 300000,
				min_carry_amount = 1000,
			}
			send_request("ttz.createtable",{table_conf = table_conf})
		elseif tonumber(string.sub(cmd,1,-2)) and string.sub(cmd,-1,-1) == "f" then
			local table_index = tonumber(string.sub(cmd,1,-2))
			send_request("ttz.entertable",{table_index = table_index})
		elseif cmd == "sg" then
			send_request("ttz.startgame",{ })
		elseif cmd == "open" then
			send_request("ttz.openlottery",{ })
		end
		
	else
		socket.usleep(100)
		if is_logined then
			if count == 100 then
				count = 0
			else
				count = count + 1
			end
		end
	end
end